<?php
namespace app\staff\controller;

use think\Request;
use think\Db;
use think\Session;
use think\Controller;
use think\Config;
use Godok\Org\Auth;
use think\Log;

class Cart extends Controller
{	
	//购物车列表
	function cartList(){
		$user_id = Request()->param('user_id');
		$list = Db::field('g.goods_id,g.goods_name,g.picture,sval.specval_id,sval.specval_name,sval.spec_name,sval.price,sval.unit,c.user_id,c.cart_id,c.num')
		->table('sdzx_goods')->alias('g')
		->join('sdzx_goods_specval sval','g.goods_id=sval.goods_id')
		->join('sdzx_user_cart c','sval.specval_id=c.specval_id and sval.goods_id=c.goods_id')
		->where("c.user_id={$user_id} and c.addbyagent>0")
		->select();
		if($list){
			$this->success('success','',$list);
		}else{
			$this->error('error');
		}
	}

	//购物车数量
	function cart_num(){
		$addbyagent = Request()->param('agent_id');
		$user_id = Request()->param('user_id');

		$cart_num = Db::name('user_cart')->where(['addbyagent'=>$addbyagent,'user_id'=>$user_id])->count('num');
		$this->success('success','',$cart_num);
	}

	/**
	 * 添加商品
	 */
	public function add()
	{
		$user_id = Request()->param('user_id');
		$specval_id = Request()->param('specval_id');
		$addbyagent = Request()->param('agent_id');
		$num = Request()->param('num');

		if (empty($specval_id) || empty($user_id) || empty($addbyagent)) {
			$this->error('缺少参数');
		}

		$res = Db::name('shop')->where('user_id',$user_id)->find();		

		//检查商品是否存在
		$issetgoods = Db::name('goods_specval')->where(['specval_id'=>$specval_id])->find();
		!$issetgoods && $this->error('商品不存在');

		//检查购物车是否存在该商品
		$incart = Db::name('user_cart')->where(['specval_id'=>$specval_id,'user_id'=>$user_id,'addbyagent'=>['neq',0]])->find();
		$id = 0;
		if ($incart) {
			if($num){
				$re = Db::name('user_cart')->where(['cart_id'=>$incart['cart_id']])->update(['num'=>$num]);
			}else{
				$re = Db::name('user_cart')->where(['cart_id'=>$incart['cart_id']])->setInc('num',1);
			}
			if ($re!==false) {
				$id = $incart['cart_id'];
			}
		}else{
			$data = [
				'user_id'    => $user_id,
				'goods_id'   => $issetgoods['goods_id'],
				'specval_id' => $specval_id,
				'add_time'   => date("Y-m-d h:i:s"),
				'shop_id'    => $res['shop_id'],
				'num'        => $num?:1,
				'addbyagent' => $addbyagent
			];
			$id = Db::name('user_cart')->insertGetId($data);
		}
		if ($id) {
			$this->success('添加成功','');
		}else{
			$this->error('添加失败');
		}
	}

	//删除
	function del(){
		$cart_id = Request()->param('cart_id');
		$res = Db::name('user_cart')->where('cart_id',$cart_id)->delete();
		if($res){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}
	}

	//用户所有地址
	function addr(){
		$user_id = Request()->param('user_id');
		$list = Db::name('user_address')->where('user_id',$user_id)->order('type desc')->select();
		if($list){
			$this->success('success','',$list);
		}else{
			$this->error('error');
		}
	}

	//添加用户地址
	function addAddress(){
		$param = Request()->param();
		!$param['user_id'] && $this->error('参数错误');
		!$param['pcd'] && $this->error('参数错误');
    	!$param['address'] && $this->error('参数错误');
    	!$param['name'] && $this->error('参数错误');
    	!$param['phone'] && $this->error('参数错误');

    	$data = [
    		'user_id'	=> $param['user_id'],
    		'pcd'		=> $param['pcd'],
    		'address'	=> $param['address'],
    		'name'		=> $param['name'],
    		'phone'		=> $param['phone']
    	];
    	$res = Db::name('user_address')->insertGetId($data);
    	if($res){
    		$this->success('添加成功','',$data);
    	}else{
    		$this->error('添加失败');
    	}
	}

	//默认地址
	function def(){
		$user_id = Request()->param('user_id');
		$obj = Db::name('user_address')->where(['user_id'=>$user_id])->order('type desc')->find();
		$this->success('success','',$obj);
	}
	//选中用户地址
	function selectAddr(){
		$address_id = Request()->param('address_id');
		$obj = Db::name('user_address')->where('address_id',$address_id)->find();
		if($obj){
			$this->success('success','',$obj);
		}else{
			$this->error('error');
		}
	}


}


